package com.cskaoyan.service.impl;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketAddressExample;
import com.cskaoyan.bean.zhangshizhi.wx.address.AddressListVO;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 地址管理
 *
 * @author little ants
 * @since 2022/06/29 20:14
 */

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    MarketAddressMapper mapper;

    /**
     * 获取当前用户的所有地址
     *
     * @param id
     * @return com.cskaoyan.bean.zhangshizhi.wx.address.AddressListVO
     * @author little ants
     * @since 2022/06/29 20:51
     */
    @Override
    public AddressListVO getAddressList(Integer id) {
        MarketAddressExample marketAddressExample = new MarketAddressExample();



        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketAddress> addressList = mapper.selectByExample(marketAddressExample);

        AddressListVO<MarketAddress> addressListVO = new AddressListVO<>();
        addressListVO.setList(addressList);
        addressListVO.setLimit(addressList.size());
        addressListVO.setTotal(addressList.size());

        return addressListVO;
    }

    /**
     * 增加该用户的地址
     *
     * @param marketAddress 新增地址信息
     * @return void
     * @author little ants
     * @since 2022/06/29 21:06
     */
    @Override
    public void saveAddress(MarketAddress marketAddress) {
        Date date = new Date();


        if (marketAddress.getIsDefault()) {
            // 修改默认地址
            MarketAddress marketAddress1 = new MarketAddress();
            marketAddress1.setIsDefault(false);
            marketAddress1.setUpdateTime(date);

            MarketAddressExample example = new MarketAddressExample();
            MarketAddressExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(marketAddress.getUserId());
            // criteria.andIsDefaultEqualTo(false);
            // criteria.andUpdateTimeEqualTo(date);
            mapper.updateByExampleSelective(marketAddress1, example);
        }


        if (marketAddress.getId() != 0) {
            mapper.updateByPrimaryKeySelective(marketAddress);
            return;
        }
        // 0代表新增地址,直接插入新地址
        marketAddress.setId(null);
        marketAddress.setAddTime(date);
        marketAddress.setUpdateTime(date);
        mapper.insertSelective(marketAddress);

    }

    /**
     * 删除该用户某个地址
     *
     * @param marketAddress 被删除的地址信息（只包含地址id）
     * @return void
     * @author little ants
     * @since 2022/06/29 21:05
     */

    @Override
    public void removeAddress(MarketAddress marketAddress) {

        MarketAddress marketAddress1 = mapper.selectByPrimaryKey(marketAddress.getId());

        marketAddress1.setUpdateTime(new Date());
        marketAddress1.setDeleted(true);

        if (!marketAddress1.getIsDefault()){
            // 不是默认地址，直接更新
            mapper.updateByPrimaryKeySelective(marketAddress1);
            return;
        }
        // 是默认地址
        marketAddress1.setIsDefault(false);
        mapper.updateByPrimaryKeySelective(marketAddress1);

        // 将查询到的第一条地址设置为默认值
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(marketAddress1.getUserId());
        criteria.andDeletedEqualTo(false);

        List<MarketAddress> addressList = mapper.selectByExample(example);
        if (addressList.size() == 0){
            return;
        }
        MarketAddress address = addressList.get(0);
        address.setIsDefault(true);
        mapper.updateByPrimaryKeySelective(address);
    }

    /**
     * 查询该用户某地址详情
     *
     * @param id
     * @return com.cskaoyan.bean.MarketAddress
     * @author little ants
     * @since 2022/06/29 21:15
     */
    @Override
    public MarketAddress getAddress(Integer id) {
        MarketAddress marketAddress = mapper.selectByPrimaryKey(id);
        return marketAddress;
    }

}
